package org.ufinity.hsms.common.utils;

import java.io.File;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Environment;


/**
 * 
 * @Description 帮助类
 * @author 钱新才
 * @version 1.0 2011-10-21
 * @class com.tydic.weibo.module.commmon.InfoHelper
 */
public class InfoHelper 
{

    public final static String SDCARD_MNT = "/mnt/sdcard";
    public final static String SDCARD = "/sdcard";
    //软件根目录
    public final static String WOWEIBO = "wo_weibo";    
    //图片存放目录
    public final static String IMAGE_FOLDER="weibo_img";
    //临时存储文件目录
    public final static String TMP="tmp";
    
    /**
     * 创建根目录和子集目录
     * @Description 
     * @param folderPath
     * @return
     * @author 钱新才
     * @version 1.0 2011-10-26
     */
    public static String createWoWeiboAndFolder(String folderPath){
    	InfoHelper.createWoWeibo();
		return FileUtils.creatFolder(folderPath);
    }
    
    /**
     * 创建项目根目录 
     * @Description 
     * @return
     * @author 钱新才
     * @version 1.0 2011-10-26
     */
    public static String createWoWeibo(){
    	//WOWEIBO根目录
    	String weiboFolder = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + WOWEIBO +File.separator;
    	return FileUtils.creatFolder(weiboFolder);
    }
    
    
	/**
	 * 使用当前时间戳拼接一个唯一的文件名
	 * @param format
	 * @return
	 */
    public static String getFileName() 
    {
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss_SS");
    	String fileName = format.format( new Timestamp( System.currentTimeMillis()) );
    	return fileName;
    }
    
    /**
     * 获取照相机使用的目录
     * @return
     */
    public static String getCamerPath()
    {
    	return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + WOWEIBO + File.separator + IMAGE_FOLDER + File.separator;
    }
    
    /**
     * 获取临时存储文件目录
     * @Description 
     * @return
     * @author 钱新才
     * @version 1.0 2011-11-9
     */
    public static String getTmpPath(){
    	return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + WOWEIBO + File.separator + TMP + File.separator;
    }
    
    public static String getImagesPath(){
    	return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + WOWEIBO + File.separator + IMAGE_FOLDER + File.separator;
    }
    
    /**
     * 获取Android的外部存储目录即 SD卡跟目录
     * @Description 
     * @return
     * @author 钱新才
     * @version 1.0 2011-10-26
     */
    public static String getSDCARD_MNT(){
    	return Environment.getExternalStorageDirectory().getAbsolutePath();
    }
    
    
	/**
	 * 判断当前Url是否标准的content://样式，如果不是，则返回绝对路径
	 * @param uri
	 * @return
	 */
	public static String getAbsolutePathFromNoStandardUri( Uri mUri )
	{	
		String filePath = null;
		
		String mUriString = mUri.toString();
		mUriString = Uri.decode(mUriString);
		
		String pre1 = "file://" + SDCARD + File.separator;
		String pre2 = "file://" + SDCARD_MNT + File.separator;
		
		if( mUriString.startsWith(pre1) )
		{    
			filePath = Environment.getExternalStorageDirectory().getPath() + File.separator + mUriString.substring( pre1.length() );
		}
		else if( mUriString.startsWith(pre2) )
		{
			filePath = Environment.getExternalStorageDirectory().getPath() + File.separator + mUriString.substring( pre2.length() );
		}
		return filePath;
	}
	
	 /**
     * 检测网络是否可用
     * @param context
     * @return
     */
    public static boolean checkNetWork(final Context context)
    {
    	boolean newWorkOK = false;  
        ConnectivityManager connectManager = (ConnectivityManager)context.getSystemService(context.CONNECTIVITY_SERVICE);
        NetworkInfo netWrokInfo = connectManager.getActiveNetworkInfo(); 
        if(netWrokInfo!=null){
        	if(netWrokInfo.isAvailable())
        	{
        		newWorkOK = true;
        	}
        }
        return newWorkOK;
    }
    
    /**
     * 检测网络是否可用(APN是否设置wap)
     * @param context
     * @return
     */
    public static boolean checkNetWorkAPN(final Context context)
    {
    	boolean isNet = false;
        ConnectivityManager connectManager = (ConnectivityManager)context.getSystemService(context.CONNECTIVITY_SERVICE);
        NetworkInfo netWrokInfo = connectManager.getActiveNetworkInfo(); 
        if(netWrokInfo!=null){
        	if(netWrokInfo.isAvailable())
        	{
        		if(netWrokInfo.getExtraInfo()!=null && netWrokInfo.getExtraInfo().toLowerCase().contains("ctc")){
        			//是中国电信的ctwap
        			isNet = true;
        		}
        	}
        }
        return isNet;
    }
    
	
	/**
	 * 根据路径获取适应屏幕大小的Bitmap
	 * @param context
	 * @param filePath
	 * @param maxWidth
	 * @return
	 */
	public static Bitmap getScaleBitmap( String filePath )
	{
		BitmapFactory.Options opts = new BitmapFactory.Options();
		opts.inSampleSize = 4;
		
		return BitmapFactory.decodeFile(filePath, opts);
	}
	
	/**
	 * 根据路径获取适应屏幕大小的Bitmap
	 * @Description 
	 * @param filePath
	 * @param inSampleSize 缩放比例
	 * @return
	 * @author 钱新才
	 * @version 1.0 2011-11-9
	 */
	public static Bitmap getScaleBitmap(String filePath , int inSampleSize )
	{
		BitmapFactory.Options opts = new BitmapFactory.Options();
		opts.inSampleSize = inSampleSize;
		return BitmapFactory.decodeFile(filePath, opts);
	}
	
}